package com.wtwd.device.service.impl;


import com.wtwd.device.constant.RespCode;
import com.wtwd.device.dao.AppDialMapper;
import com.wtwd.device.model.entity.DeviceSetting;
import com.wtwd.device.model.entity.DialManagement;
import com.wtwd.device.model.entity.DialPlate;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.service.AppDialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author mjy
 * @date 2021/08/11
 * @description
 */
@Service
public class AppDialServiceImpl implements AppDialService {
    @Autowired
    private AppDialMapper appDialMapper;

    @Value("${file.http}")
    private String fileHttp;
    @Value("${dial.preview.image}")
    private String previewImg;
    @Value( "${product.image}")
    private String imgPlace;
    @Value("${dial.file}")
    private String dialFile;

    @Override
    public RespEntity<DialPlate> getDefaultDialList(Integer currentPage, Integer pageSize, String watchId) {
        Integer start = currentPage * pageSize;
        List<DialPlate> dialPlates = appDialMapper.getDefaultDialList(start, pageSize, watchId);
        dialPlates.stream().forEach(e -> {
            String url = fileHttp.concat(previewImg.concat(e.getPreviewImg()));
            e.setPreviewImg(url);
            String fileUrl=fileHttp.concat(dialFile.concat(e.getDialFile()));
            e.setDialFile(fileUrl);
        });
        return new RespEntity(RespCode.SUCCESS, dialPlates);
    }

    @Override
    public RespEntity<DialPlate> getDefaultDialInfo(Integer dialId) {
        DialPlate dialPlate = appDialMapper.getDefaultDialInfo(dialId);
        if (dialPlate!=null){
            dialPlate.setPreviewImg(fileHttp.concat(previewImg.concat(dialPlate.getPreviewImg())));
            String fileUrl=fileHttp.concat(dialFile.concat(dialPlate.getDialFile()));
            dialPlate.setDialFile(fileUrl);
        }
        return new RespEntity(RespCode.SUCCESS, dialPlate);
    }

    @Override
    public ResultVO<List<DialManagement>> getDialListByWatchId(Integer currentPage, Integer pageSize, String watchId) {
        List<DialManagement> dialManagements = appDialMapper.getAllDialList();
        Iterator<DialManagement> iterator = dialManagements.iterator();
        while(iterator.hasNext()){
            DialManagement dialManagement = iterator.next();
            String watchIds = dialManagement.getWatchIds();
            if (watchIds!=null&&watchIds.length()>0){
                String[] split = watchIds.split(",");
                List<String> idList = Arrays.asList(split);
                //定义存在标志
                boolean flag=false;
                for (String id : idList) {
                  if (id.equals(watchId)){
                      flag=true;
                      break;
                  }
                }
              if (!flag){
                 iterator.remove();
                 continue;
              }else {
                  String url = fileHttp.concat(previewImg.concat(dialManagement.getPreviewImg()));
                  String fileUrl=fileHttp.concat(dialFile.concat(dialManagement.getDialFile()));
                  dialManagement.setPreviewImg(url);
                  dialManagement.setDialFile(fileUrl);
              }
            }
        }
        return  ResultVO.success(disposalData(dialManagements,currentPage,pageSize)) ;
    }

    @Override
    public RespEntity<DeviceSetting> getDeviceSetting(String watchId) {
        DeviceSetting deviceSetting = appDialMapper.getDeviceSettingByWatchId(watchId);
        if (deviceSetting!=null){
            deviceSetting.setImgName(fileHttp.concat(imgPlace.concat(deviceSetting.getImgName())));
        }
        return new RespEntity(RespCode.SUCCESS,deviceSetting);
    }


    public  <T> List<T> disposalData(List<T> list, Integer curPage, Integer pageSize) {
        List<T> resultRecord = new ArrayList<>();
        Integer total = list.size();
        int start = curPage * pageSize;
        int end = start + pageSize;
        if(list.size()<start){
            return resultRecord;
        }
        for (int i = start; i < (list.size()<end?list.size():end); i++) {
            resultRecord.add(list.get(i));
        }
        return resultRecord;
    }

}
